package cn.openea.perpartner.meily.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.meily.util.UserContext;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.openea.perpartner.meily.web.mapper.ClienteleMapper;
import cn.openea.perpartner.meily.web.entity.Clientele;
import cn.openea.perpartner.meily.web.service.IClienteleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (ClienteleServices)表服务实现类
 *
 * @author Weird077
 * @since 2023-01-11 18:57:21
 */
@Service("clienteleServicesService")
public class ClienteleServiceImpl extends ServiceImpl<ClienteleMapper, Clientele> implements IClienteleService {
    @Override
    public List<Clientele> selectList(Clientele clientele) {
        LambdaQueryWrapper<Clientele> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(clientele.getCsId())) {
            wrapper.like(Clientele::getCsId, clientele.getCsId());
        }
        if (StrUtil.isNotBlank(clientele.getCsType())) {
            wrapper.like(Clientele::getCsType, clientele.getCsType());
        }
        if (StrUtil.isNotBlank(clientele.getCsName())) {
            wrapper.like(Clientele::getCsName, clientele.getCsName());
        }
        if (StrUtil.isNotBlank(clientele.getSceneId())) {
            wrapper.like(Clientele::getSceneId, clientele.getSceneId());
        }
        if (StrUtil.isNotBlank(clientele.getStatus())) {
            wrapper.like(Clientele::getStatus, clientele.getStatus());
        }
        if (StrUtil.isNotBlank(clientele.getUserId())) {
            wrapper.like(Clientele::getUserId, clientele.getUserId());
        }
        wrapper.orderByDesc(Clientele::getUpdateTime);
        return list(wrapper);
    }

    @Override
    public boolean add(Clientele clientele) {

        if(StrUtil.isBlank(clientele.getCsId())){
            LambdaQueryWrapper<Clientele> wrapper = new LambdaQueryWrapper<>();
            wrapper.orderByDesc(Clientele::getCreateTime);
            wrapper.last("limit 1");
            Clientele kf =getOne(wrapper);
            if (BeanUtil.isEmpty(kf)) {
                clientele.setCsId("KF-0001");
            } else {
                String csId = kf.getCsId();
                String numSection = csId.substring(3);
                int newNum = Integer.parseInt(numSection) + 1;
                if(newNum > 0 && newNum < 10){
                    csId = "KF-000" + newNum;
                }
                if(newNum >= 10 && newNum < 100){
                    csId = "KF-00" + newNum;
                }
                if(newNum >= 100 && newNum < 1000){
                    csId = "KF-0" + newNum;
                }
                if(newNum >= 1000 && newNum < 10000){
                    csId = "KF-" + newNum;
                }
                clientele.setCsId(csId);
            }
        }
        clientele.setCreateUser(UserContext.getUserId());
        clientele.setUpdateUser(UserContext.getUserId());
        return save(clientele);
    }

    @Override
    public Clientele selectByCsId(String csId) {
        LambdaQueryWrapper<Clientele> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Clientele::getCsId, csId);
        return getOne(wrapper,false);
    }
}

